#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mysignal.h"
#include <sys/ioctl.h>

#define DEVICE_PATH "/dev/mycdev_simple0"

int main(int argc, char const *argv[])
{
    int fd;
    char buf[1024];
    int len;
    int ret;
    /*判断参数*/
    if (argc < 2)
    {
        printf("Usage: %s -w <string> \n", argv[0]);
        printf("       %s -r \n", argv[0]);
        return -1;
    }
    /*打开文件*/
    fd = open(DEVICE_PATH, O_RDWR);
    if (fd == -1)
    {
        printf("cannot open file %s\n", DEVICE_PATH);
        return -1;
    }
    printf("open file %s 0000k\n", DEVICE_PATH);

    /*写文件或者读文件*/
    if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
    {
        // 是写入数据，且给了数据
        len = strlen(argv[2]) + 1;
        len = len < 1024 ? len : 1024;
        ret = write(fd, argv[2], len);
        printf("write driver: %d\n", ret);
    }
    else if (0 == strcmp(argv[1], "-r"))
    {
        len = read(fd, buf, 1024);
        buf[1023] = '\0';
        printf("APP read: %s\n", buf);
    }
    else if (0 == strcmp(argv[1], "m1"))
    {
        ioctl(fd, PRINT_MODE_1);
        printf("want to switch print mode to normal\n");
    }
    else if (0 == strcmp(argv[1], "m2"))
    {
        ioctl(fd, PRINT_MODE_2);
        printf("want to switch print mode to reverse\n");
    }
    close(fd);
    return 0;
}
